<template>
  <span 
    class="base-tag" 
    :class="[
      `base-tag-${type}`, 
      `base-tag-${size}`, 
      { 'base-tag-closable': closable }
    ]"
    :style="tagStyle"
  >
    <span class="base-tag-content">
      <slot></slot>
    </span>
    <span 
      v-if="closable" 
      class="base-tag-close-icon" 
      @click.stop="handleClose"
    >
      <svg width="10" height="10" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M5 3.879L7.94 0.94L9.06 2.06L6.121 5L9.06 7.94L7.94 9.06L5 6.121L2.06 9.06L0.94 7.94L3.879 5L0.94 2.06L2.06 0.94L5 3.879Z" fill="currentColor" />
      </svg>
    </span>
  </span>
</template>

<script>
export default {
  name: 'BaseTag',
  props: {
    type: {
      type: String,
      default: 'default',
      validator: (value) => {
        return ['default', 'primary', 'success', 'warning', 'danger', 'info'].includes(value);
      }
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => {
        return ['small', 'medium', 'large'].includes(value);
      }
    },
    color: {
      type: String,
      default: ''
    },
    closable: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    tagStyle() {
      if (!this.color) return {};
      
      const colorMap = {
        background: this.color,
        borderColor: this.color,
        color: this.isLightColor(this.color) ? '#595959' : '#ffffff'
      };
      
      return colorMap;
    }
  },
  methods: {
    handleClose(event) {
      this.$emit('close', event);
    },
    isLightColor(color) {
      if (!color) return true;
      
      // 简单评估颜色是否为浅色
      const hex = color.replace('#', '');
      const r = parseInt(hex.substr(0, 2), 16);
      const g = parseInt(hex.substr(2, 2), 16);
      const b = parseInt(hex.substr(4, 2), 16);
      
      return (r * 0.299 + g * 0.587 + b * 0.114) > 160;
    }
  }
}
</script>

<style scoped>
.base-tag {
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  padding: 0 8px;
  font-size: 12px;
  line-height: 20px;
  box-sizing: border-box;
  white-space: nowrap;
  border: 1px solid transparent;
}

.base-tag-content {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 尺寸 */
.base-tag-small {
  padding: 0 6px;
  font-size: 10px;
  line-height: 16px;
  border-radius: 2px;
}

.base-tag-medium {
  padding: 0 8px;
  font-size: 12px;
  line-height: 20px;
  border-radius: 4px;
}

.base-tag-large {
  padding: 0 10px;
  font-size: 14px;
  line-height: 24px;
  border-radius: 4px;
}

/* 类型 */
.base-tag-default {
  background-color: #f5f5f5;
  border-color: #f0f0f0;
  color: #595959;
}

.base-tag-primary {
  background-color: #e6f7ff;
  border-color: #91d5ff;
  color: #1890ff;
}

.base-tag-success {
  background-color: #f6ffed;
  border-color: #b7eb8f;
  color: #52c41a;
}

.base-tag-warning {
  background-color: #fffbe6;
  border-color: #ffe58f;
  color: #faad14;
}

.base-tag-danger {
  background-color: #fff1f0;
  border-color: #ffa39e;
  color: #f5222d;
}

.base-tag-info {
  background-color: #f0f5ff;
  border-color: #adc6ff;
  color: #2f54eb;
}

/* 关闭按钮 */
.base-tag-closable {
  padding-right: 4px;
}

.base-tag-close-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
  width: 16px;
  height: 16px;
  color: currentColor;
  opacity: 0.6;
  cursor: pointer;
  transition: opacity 0.2s;
}

.base-tag-close-icon:hover {
  opacity: 1;
}
</style> 